// 
// ExtenionMethods.cs
//  
// Author:
//       Tomáš Vondráček <tomas.vondracek@gmail.com>
// 
// Copyright (c) 2010 Tomas Vondracek
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Common.Gui.GuiWinFormsLib.Helpers
{
	public static class ExtensionMethods
	{
		/// <summary>
		/// Draw Rectangle with rounded corners
		/// </summary>
		/// <param name="g">
		/// A <see cref="Graphics"/>
		/// </param>
		/// <param name="p">
		/// A <see cref="Pen"/>
		/// </param>
		/// <param name="x">
		/// A <see cref="System.Single"/>
		/// </param>
		/// <param name="y">
		/// A <see cref="System.Single"/>
		/// </param>
		/// <param name="width">
		/// A <see cref="System.Single"/>
		/// </param>
		/// <param name="height">
		/// A <see cref="System.Single"/>
		/// </param>
		/// <param name="radius">
		/// A <see cref="System.Single"/> angle in degrees
		/// </param>
		public static void DrawRoundRect(this Graphics g, Pen p, float x, float y, float width, float height, float radius)
		{
		   GraphicsPath gp = new GraphicsPath();
		 
		   gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
		   gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner
		   gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Line
		   gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
		   gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Line
		   gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
		   gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line
		   gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
		   gp.CloseFigure();
		 
		   g.DrawPath(p, gp);
		   gp.Dispose();
		}
		
		public static void FillRoundRect(this Graphics g, Brush brush, float x, float y, float width, float height, float radius)
		{
		   GraphicsPath gp = new GraphicsPath();
		 
		   gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
		   gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner
		   gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Line
		   gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
		   gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Line
		   gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
		   gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line
		   gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
		   gp.CloseFigure();
		 
		   g.FillPath(brush, gp);
		   gp.Dispose();
		}
	}
}

